/**
 * Copyright (C) 2010, 2011 Neofonie GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.dicode.analysis.demonstrator.model;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

import com.google.common.base.Preconditions;

/**
 * This class holds the configuration of one topic as a set of queries that
 * should be OR-ed and sent to the search service.
 * */
public class TopicCommand {
  /** Name of the topic to configure. */
  private final String topic;
  /** The set of queries representing this topic. */
  private final Set<String> queries;

  /**
   * Init topic with empty query set.
   * 
   * @param topic
   *          the string used to refer to this topic.
   * */
  public TopicCommand(final String topic) {
    Preconditions.checkNotNull(topic);
    Preconditions.checkArgument(!topic.isEmpty());

    this.topic = topic;
    this.queries = new TreeSet<String>();
  }

  /** Copy constructor. */
  public TopicCommand(final TopicCommand topic) {
    Preconditions.checkNotNull(topic);

    this.topic = topic.getTopic();
    this.queries = new TreeSet<String>();
    this.queries.addAll(topic.queries);
  }

  /** From xsd topic constructor. */
  public TopicCommand(final eu.dicodeproject.analysis.xsd.topics.Topic topic) {
    Preconditions.checkNotNull(topic);
    Preconditions.checkNotNull(topic.getTopic());
    Preconditions.checkNotNull(topic.getQueries());
    Preconditions.checkNotNull(topic.getQueries().getQuery());
    Preconditions.checkArgument(!topic.getTopic().isEmpty());
    Preconditions.checkArgument(!(topic.getQueries().getQuery().size() == 0));

    this.topic = topic.getTopic();
    this.queries = new TreeSet<String>();
    this.queries.addAll(topic.getQueries().getQuery());
  }

  /** Returns the name used to refer to this topic. */
  public String getTopic() {
    return topic;
  }

  /** Reset the set of queries to the given set. */
  public synchronized void setQueries(final Collection<String> queries) {
    Preconditions.checkNotNull(queries);
    Preconditions.checkArgument(!(queries.size() == 0));

    this.queries.clear();
    this.queries.addAll(queries);
  }

  /**
   * Reset the set of queries to the set given as formatted string - each query
   * separated from it's neighbors by a comma. Example: "maggi,knorr,neofonie"
   */
  public synchronized void setQueries(final String queries) {
    Preconditions.checkNotNull(queries);
    Preconditions.checkArgument(!queries.isEmpty());

    this.queries.clear();
    String[] splitedQueries = queries.split(",");
    for (String query : splitedQueries) {
      query = query.trim();
      if (query.length() > 0) {
	this.queries.add(query);
      }
    }
  }

  /** Return a non-modifiable collections of the queries for this topic. */
  public synchronized Collection<String> getQueries() {
    return Collections.unmodifiableCollection(queries);
  }

  // DOWN HERE AUTO GENERATED BY ECLIPSE BASED ON THE TOPIC STRING ONLY //

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((topic == null) ? 0 : topic.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    TopicCommand other = (TopicCommand) obj;
    if (topic == null) {
      if (other.topic != null) {
	return false;
      }
    } else if (!topic.equals(other.topic)) {
      return false;
    }
    return true;
  }

}
